package com.dream.myqiyi.ui;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.dream.myqiyi.Global;
import com.dream.myqiyi.R;
import com.dream.myqiyi.dao.NotebookModel;
import com.dream.myqiyi.service.ApplicationContext;
import com.dream.myqiyi.service.NotebookService;

public class Login extends Activity {
	private EditText mUsername = null; // 帐号编辑框 
	private EditText mPassword = null; // 密码编辑框 
	private CheckBox mAutoLogin = null;
	private SharedPreferences sp = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        sp = this.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        
        mUsername = (EditText)findViewById(R.id.login_user_edit);
        mPassword = (EditText)findViewById(R.id.login_passwd_edit);
        mAutoLogin = (CheckBox)findViewById(R.id.autologin);
        
    	mUsername.setText(sp.getString("username", ""));
    	mPassword.setText(sp.getString("password", "")); 
    	mAutoLogin.setChecked(sp.getBoolean("autologin", false));
    }
    
    private void login(String username, String password) {
    	if ("note".equals(username) && "123".equals(password)) {
			boolean autoLogin = mAutoLogin.isChecked();
			if (autoLogin) {
				Editor editor = sp.edit();
				editor.putString("username", username);
				editor.putString("password", password);
				editor.putBoolean("autologin", true);
				editor.commit();
			} else {  
				Editor editor = sp.edit();
				editor.putString("username", "");
				editor.putString("password", "");
				editor.putBoolean("autologin", false);
				editor.commit();
			}
			
			Global.username = username;
    		Intent intent = new Intent();
    		intent.setClass(Login.this,LoadingActivity.class);
    		startActivity(intent);
    		Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
    		
    		// 增加用户名的默认笔记本
    		NotebookService nbSrv = (NotebookService)ApplicationContext.getSingleton().getBean("NotebookService");
    		NotebookModel nb = nbSrv.addNotebook(username + "的笔记");
    		nbSrv.setCurrNotebook(nb);
    		
    	} else if("".equals(username) || "".equals(password)) {
         	new AlertDialog.Builder(Login.this)
			.setIcon(getResources().getDrawable(R.drawable.login_error_icon))
			.setTitle("登录错误")
			.setMessage("印象笔记帐号或者密码不能为空，\n请输入后再登录！")
			.create().show();
         } else {
        	new AlertDialog.Builder(Login.this)
			.setIcon(getResources().getDrawable(R.drawable.login_error_icon))
			.setTitle("登录失败")
			.setMessage("印象笔记帐号或者密码不正确，\n请检查后重新输入！")
			.create().show();
    	}
    }

    public void login_mainweixin(View v) {
    	login(mUsername.getText().toString(), mPassword.getText().toString());
    	
    	//登录按钮 
    	/*
      	Intent intent = new Intent();
		intent.setClass(Login.this,Whatsnew.class);
		startActivity(intent);
		Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
		this.finish();*/
      }  
    public void login_back(View v) {     //标题栏 返回按钮
      	this.finish();
      }  
    public void login_pw(View v) {     //忘记密码按钮    	
    	Uri uri = Uri.parse("http://3g.qq.com"); 
    	Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
    	startActivity(intent);
    	//Intent intent = new Intent();
    	//intent.setClass(Login.this,Whatsnew.class);
        //startActivity(intent);
      }  
}
